Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

265
Visualizações
returning [object HTMLDivElement] when using document.getElementById

I am inserting this into my html div:

<script>
    document.getElementById("mArray").appendChild = '<%= mArray %>'
    console.log('<%= mArray %>')

  </script>
  <div id="mArray"></div>

then, inside my js file, I have this:

var mArray = document.getElementById("mArray")
console.log("33: " + mArray)

but when I log it, I get this:

33: [object HTMLDivElement]

i want it to return the array data, which should look like [1,3,5,7,9]

what am i doing wrong?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

When reading, use

var mArray = document.getElementById("mArray").textContent

and if you want to use it as a JS array (assuming it is a valid js array format), you could do

var mArrayContents = document.getElementById("mArray").textContent;
var myArray = JSON.parse( mArrayContents );

Additionally, when setting the value you do not have to use JS.
You can do it directly

<div id="mArray"><%= mArray %></div>
about 4 years ago · Juan Pablo Isaza Relatório

0

Right now you are logging the element itself, you need to check for the data inside. Depending on how the information is stored you could possibly get the information with something such as

document.getElementById("mArray").innerHTML;

or

document.getElementById("mArray").value;
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda